using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework.Graphics;

namespace Spinning.Spinning.Entity
{
    class ScrollScene : BaseScene
    {
        RenderTarget2D temp;
        GraphicsDevice gd;
        Matrix projection;
        SpriteBatch _spriteBatch;


        public ScrollScene()
        {
            projection = Matrix.Identity;
        }

        public override void OnLoad()
        {
            base.OnLoad();

            this.gd = Game1.graphicsDevice;
            temp = new RenderTarget2D(gd, (int)Size.X, (int)Size.Y);
        }

        protected override void UpdateSelf(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            if (_spriteBatch != null)
            {
                preDrawFrame(_spriteBatch, gameTime);
            }
        }

        public override Matrix GetProjectionMatrix()
        {
            return projection;
        }

        protected override Matrix UnProjectionMatrix(Interface.IEntity entity)
        {
            Vector2 entityPos = entity.Position;
            Vector2 oz = Size * Origin;
            Matrix translateBackToPosition = Matrix.CreateTranslation(-entityPos.X+oz.X, -entityPos.Y+oz.Y, 0);

            Matrix compositeMatrix = translateBackToPosition;
            return compositeMatrix;
        }

        protected override void DrawSelf(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.GameTime gameTime)
        {
            _spriteBatch = spriteBatch;

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp,
                DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Projection);
            spriteBatch.Draw(temp, this.Position, null, Color.White, this.Rotation, this.Origin * this.Size, this.Scale, SpriteEffects.None, 0);
            spriteBatch.End();
        }

        private void preDrawFrame(SpriteBatch spriteBatch, Microsoft.Xna.Framework.GameTime gameTime)
        {
            RenderTarget2D previousTarget = null;
            if (gd.GetRenderTargets().Length > 0)
            {
                previousTarget = (RenderTarget2D)gd.GetRenderTargets()[0].RenderTarget;
            }
            gd.SetRenderTarget(temp);
            gd.Clear(Color.Transparent);
            drawSelf(spriteBatch, gameTime);
            base.drawChildren(spriteBatch, gameTime);
            gd.SetRenderTarget(previousTarget);
        }

        protected override void drawChildren(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //base.drawChildren(spriteBatch, gameTime);
        }
        

        private void drawSelf(SpriteBatch spriteBatch, Microsoft.Xna.Framework.GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, projection);
            spriteBatch.Draw(_backTexture, Vector2.Zero, null, this.Color, 0, Vector2.Zero, Size, SpriteEffects.None, 0);
            spriteBatch.End();
            
        }

        public override BaseScene Clone()
        {
            throw new NotImplementedException();
        }
    }
}
